package com.validant.enigma3.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import junit.framework.Assert;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.openqa.selenium.By;

/*
 * This class provides functions related to mapping UI controls
 */
public class UIMap {

	private HashMap<String, MappingObj> controls;
	
	public UIMap(String filepath, String sheetname)
	{
		try{
			this.controls = new HashMap<String, MappingObj>();
			this.controls = this.readControls(filepath, sheetname);
		}
		catch (IOException error)
		{
			System.out.println(error);
			Assert.assertTrue(false);
		}
	}
	
	public By getElement(String key)
	{
		if (this.controls.get(key).getSelector().toLowerCase()
				.equals("cssselector")) {
			return By.cssSelector(this.controls.get(key).getPath());
		} else if (this.controls.get(key).getSelector().toLowerCase()
				.equals("xpath")) {
			return By.xpath(this.controls.get(key).getPath());
		} else if (this.controls.get(key).getSelector().toLowerCase()
				.equals("name")) {
			return By.name(this.controls.get(key).getPath());
		} else if (this.controls.get(key).getSelector().toLowerCase()
				.equals("id")) {
			return By.id(this.controls.get(key).getPath());
		} else if (this.controls.get(key).getSelector().toLowerCase()
				.equals("classname")) {
			return By.className(this.controls.get(key).getPath());
		} else if (this.controls.get(key).getSelector().toLowerCase()
				.equals("tagname")) {
			return By.tagName(this.controls.get(key).getPath());
		} else if (this.controls.get(key).getSelector().toLowerCase()
				.equals("linktext")) {
			return By.linkText(this.controls.get(key).getPath());
		} else
			return By.cssSelector(this.controls.get(key).getPath());
	}
	
	private class MappingObj{
		String codename = "";
		String selector = "";
		String path = "";

		private String getCodeName() {
			return this.codename;
		}

		private String getSelector() {
			return this.selector;
		}

		private String getPath() {
			return this.path;
		}

		private String setCodeName(String codename) {
			return this.codename = codename;
		}

		private String setSelector(String selector) {
			return this.selector = selector;
		}

		private String setPath(String path) {
			return this.path = path;
		}
	}

	private HashMap<String, MappingObj> readControls(String filepath, String sheetname)
			throws IOException {

		HashMap<String, MappingObj> controllist = new HashMap<String, MappingObj>();

		try {
			FileInputStream file = new FileInputStream(filepath);

			/** Create a POIFSFileSystem object **/
			POIFSFileSystem myFileSystem = new POIFSFileSystem(file);

			/** Create a workbook using the File System **/
			HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

			/** Get the first sheet from workbook **/
			HSSFSheet mySheet = myWorkBook.getSheet(sheetname);

			/**
			 * We now need something to iterate through the cells. Iterate
			 * through each rows from first sheet
			 **/
			Iterator<Row> rowIter = mySheet.rowIterator();
			rowIter.next();

			String[] arow = new String[3];

			while (rowIter.hasNext()) {
				HSSFRow row = (HSSFRow) rowIter.next();
				Iterator<Cell> cells = row.cellIterator();

				int i = 0;

				// Check data from current cell, there is data on next cell or
				// not?
				while (cells.hasNext()) {

					HSSFCell cell = (HSSFCell) cells.next();
					arow[i] = cell.getStringCellValue();
					i++;
				}

				MappingObj mapObj = new MappingObj();

				mapObj.setCodeName(arow[0]);
				mapObj.setSelector(arow[1]);
				mapObj.setPath(arow[2]);

				controllist.put(arow[0], mapObj);
				file.close();
			}

		}catch (FileNotFoundException error){
			System.out.println(error);
			Assert.assertTrue(false);
		}
		return controllist;
	}		
}

